package com.hc.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.music.common.R;
import com.hc.music.dao.SongMapper;
import com.hc.music.entity.Singer;
import com.hc.music.entity.Song;
import com.hc.music.service.SongService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Service
public class SongServiceImpl extends ServiceImpl<SongMapper,Song> implements SongService {
    @Autowired
    SongMapper songMapper;

    @Override
    public R insert(Song song, MultipartFile lrcfile, MultipartFile file) {
        try {
            long time = System.currentTimeMillis();
            song.setImage("img/songPic/user.jpg");
            if(lrcfile!=null && song.getLrc().equals("[00:00:00]暂无歌词")){
                String content = new String(lrcfile.getBytes(), "utf-8");
                song.setLrc(content);
            }
            String f = time + file.getOriginalFilename();
            String url = "song/" + f;
            if(!file.isEmpty()){
                String fPath = System.getProperty("user.dir")+System.getProperty("file.separator")+"song"+System.getProperty("file.separator")+ f;
                File fFile = new File(fPath);
                file.transferTo(fFile);
                song.setUrl(url);
            }else {
                return R.error("上传失败");
            }
            if(songMapper.insert(song) > 0 ){
                return R.success("上传成功",url);
            }else {
                return R.error("上传失败");
            }
        } catch (IOException e) {
            return R.error("上传失败");
        }
    }

    @Override
    public R updateLrc(MultipartFile lrc, int id) {
        try {
            if(!lrc.isEmpty()){
                Song song = songMapper.selectById(id);
                String content = new String(lrc.getBytes(), "utf-8");
                song.setLrc(content);
                if(songMapper.updateById(song) > 0){
                    return R.success("更新成功");
                }else {
                    return R.error("更新失败");
                }
            }else {
                return R.error("更新失败");
            }
        }catch (Exception e){
            return R.error("更新失败");
        }
    }

    @Override
    public R update(Song song) {
        int i = songMapper.updateById(song);
        if(i > 0){
            return R.success("修改成功");
        }else {
            return R.error("修改失败");
        }
    }

    @Override
    public R updateUrl(MultipartFile file, int id) {
        try {
            deleteLocalFile(id);
            long time = System.currentTimeMillis();
            String f = time + file.getOriginalFilename();
            String url = "song/" + f;
            Song song = new Song();
            song.setId(id);
            song.setUrl(url);
            if(!file.isEmpty()){
                String fPath = System.getProperty("user.dir")+System.getProperty("file.separator")+"song"+System.getProperty("file.separator")+ f;
                File fFile = new File(fPath);
                file.transferTo(fFile);

            }else {
                return R.error("更新失败");
            }
            if(songMapper.updateById(song) > 0){
                return R.success("更新成功");
            }else {
                return R.error("更新失败");
            }
        }catch (Exception e){
            return R.error("更新失败");
        }
    }

    @Override
    public R updateImage(MultipartFile url, int id) {
        try {
            deleteSongPic(id);
            String filename = System.currentTimeMillis() + url.getOriginalFilename();
            String path = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"+System.getProperty("file.separator")+"songPic" +System.getProperty("file.separator")+ filename;
            File file = new File(path);
            url.transferTo(file);
            Song singer = new Song();
            singer.setId(id);
            String rp = "img/songPic/"+ filename;
            singer.setImage(rp);
            if(songMapper.updateById(singer) > 0){
                return R.success("上传成功",rp);
            }else {
                return R.error("上传失败");
            }
        } catch (IOException e) {
            return R.error("上传失败");
        }
    }

    @Override
    public R delete(Integer id) {
        deleteLocalFile(id);
        if(songMapper.deleteById(id) > 0){
            return R.success("删除成功");
        }else {
            return R.error("删除失败");
        }
    }

    private void deleteSongPic(Integer id){
        String url = songMapper.selectById(id).getImage();
        if(!url.contains("user.jpg")){
            String name = url.substring(12);
            String fPath = System.getProperty("user.dir")+System.getProperty("file.separator")+"img"+System.getProperty("file.separator")+"songPic"+System.getProperty("file.separator")+name;
            File fFile = new File(fPath);
            if(fFile.exists()){
                fFile.delete();
            }
        }
    }
    private void deleteLocalFile(Integer id){
        String url = songMapper.selectById(id).getUrl();
        String name = url.substring(5);
        String fPath = System.getProperty("user.dir")+System.getProperty("file.separator")+"song"+System.getProperty("file.separator")+name;
        File fFile = new File(fPath);
        if(fFile.exists()){
            fFile.delete();
        }
    }

    @Override
    public R selectAll() {
        return R.success(null,songMapper.selectList(null));
    }

    @Override
    public R selectBySingerId(Integer singerId) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("singer_id",singerId);
        return R.success(null,songMapper.selectList(queryWrapper));
    }

    @Override
    public R selectById(Integer id) {
        return R.success(null,songMapper.selectById(id));
    }

    @Override
    public R selectBySingerName(String name) {
        QueryWrapper<Song> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("name",name);
        List<Song> songList = songMapper.selectList(queryWrapper);
        if(!songList.isEmpty()){
            return R.success(null,songList);
        }else {
            return R.error("查询失败");
        }
    }
}
